﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eCitizen.eAlerts.Classes;

using eCitizen.eAlerts.Utilities;


namespace eCitizen.eAlerts.BusinessService
{
    public class RequestManager
    {

        private ServiceInfo _currentServiceInfo;

        public RequestManager(ServiceInfo currentServiceInfo)
        {
            _currentServiceInfo = currentServiceInfo;
        }
        /// <summary>
        /// This method will process a pair of SendAlertService files, validate then return a list of RequestEntry if successful
        /// </summary>
        /// <param name="eAlertRecord"></param>
        /// <param name="checkSumRecord"></param>
        /// <param name="eAlertRequests"></param>
        /// <returns></returns>
        public bool TryParseSendServiceAlerts(ServiceRequest serviceRequest,out List<RequestEntry> eAlertRequests)
        {
            bool isParseSuccessful = true;
            int recordCount = 0;

            List<RequestEntry> requestList = new List<RequestEntry>();
            RequestParser alertParser = new RequestParser(_currentServiceInfo);

            isParseSuccessful = alertParser.TryParseCheckSum(serviceRequest.ChecksumFile, out recordCount);

            if (isParseSuccessful) isParseSuccessful = alertParser.TryParseRequestRecords(serviceRequest.RequestFile, out requestList);

            if (isParseSuccessful) isParseSuccessful = alertParser.ValidateParsedRecords(requestList, recordCount);

            eAlertRequests = requestList;
            return isParseSuccessful;
        }

        public bool TryParseSendReportRecords(ServiceRequest serviceRequest, out SmsStatusReport statusReport)
        {
            bool isParseSuccessful = true;
            SmsStatusReport smsStatusReport = new SmsStatusReport();

            RequestParser requestParser = new RequestParser(_currentServiceInfo);

            isParseSuccessful = requestParser.TryParseSmsReportRecords(serviceRequest.RequestFile, out smsStatusReport);

            statusReport = smsStatusReport;
            return isParseSuccessful;
                
        }

    }
}
